<?php

/*
 * +----------------------------------------------------------------------
 * 作 者: xinglou <xinglou1@foxmail.com>
 * +----------------------------------------------------------------------
 * 创建日期：2015-06-24
 * +----------------------------------------------------------------------
 * 文件描述：系统选项管理
 * +----------------------------------------------------------------------
 * 升级记录：
 * +----------------------------------------------------------------------
 */
class ClassAction extends CommonAction
{

    /**
     * 显示页面
     */
    public function index()
    {
        $model = new ClassModel();
        
        // 条件检索
        if (! empty($_REQUEST['search_keyword'])) {
            $where['name|code'] = array(
                'like',
                '%' . $this->_request('search_keyword') . '%'
            );
        }
        
        // 分页处理
        import('ORG.Util.Page'); // 导入分页类
        $count = $model->where($where)->count(); // 查询满足要求的总记录数
        $p = $this->_page($model, 'update_date', 'desc'); // 分页所需信息
        $page = new Page($count, $p['listRows']); // 实例化分页类 传入总记录数和每页显示的记录数
        $data = $model->field("id,
											name,
											code,
											schoolid,
											get_user_name(operator) operator,
											update_date")
            ->where($where)
            ->order("`" . $p['order'] . "` " . $p['sort'])
            ->limit($page->firstRow . ', ' . $page->listRows)
            ->select();
        $this->assign('list', $data);
        $show = $page->show(); // 分页显示输出
        $this->assign('show', $show); // 绑定分页显示字符串
        $school_list = $this->get_school_list();
        $this->assign('school_list', $school_list);
        $this->display();
    }

    /**
     * 显示添加页面
     */
    public function add()
    {
        $school_list = $this->get_school_list();
        $this->assign('school_list', $school_list);
        $this->display();
    }

    /**
     * 将添加的信息写入数据库
     */
    public function insert()
    {
        $model = new ClassModel();
        $data = $model->create();
        if (false !== $data) {
            if (false !== $model->add()) {
                $this->success('添加成功', __APP__ . '?g=Admin&m=Class&a=index');
            } else {
                $this->error('添加失败');
            }
        } else {
            $this->error($model->getError());
        }
    }

    /**
     * 显示修改页面
     */
    public function edit()
    {
        $id = $this->_request('id');
        $model = new ClassModel();
        $data = $model->field("id,
				                        code,
				                        name,
										schoolid")
            ->where("id = '" . $id . "'")
            ->find();
        $this->assign('list', $data);
        $school_list = $this->get_school_list();
        $this->assign('school_list', $school_list);
        
        $this->display();
    }

    /**
     * 将修改的信息更新到数据库
     */
    public function update()
    {
        $model = new ClassModel();
        $data = $model->create();
        if (false !== $data) {
            $data = $model->save();
            if (false !== $data) {
                $this->success('修改成功', __APP__ . '?g=Admin&m=Class&a=index');
            } else {
                $this->error('修改失败');
            }
        } else {
            $this->error($model->getError());
        }
    }

    /**
     * 删除信息
     */
    public function delete()
    {
        $deleteId = $this->_request('ids');
        if (! empty($deleteId) && is_array($deleteId)) {
            $model = new ClassModel();
            $id = implode("','", $deleteId); // 用','分割id数组，以便于拼装sql语句
            $id = "'" . $id . "'"; // 拼装sql语句，结果为'firstId','secondId',…
            $this->error($id);
            if ($data = $model->where("id in(" . $id . ")")->delete()) {
                redirect($_SERVER['HTTP_REFERER']);
            } else {
                $this->error('删除失败！');
            }
        } else {
            $this->error('您未选中需要删除的信息');
        }
    }

    /**
     * 组装数据
     */
    public function make_data()
    {
        $data = "Hello World~~~";
        dump($data);
        echo "------------------------------我是华丽丽的分割线------------------------------------<br/>";
        $data = array(
            "a" => "111",
            "b" => "222",
            "c" => "333"
        );
        dump($data);
        $this->assign("data1", $data);
        echo "------------------------------我是华丽丽的分割线------------------------------------<br/>";
        $data = array(
            "a" => array(
                "a" => "111111aa",
                "b" => "222222aa"
            ),
            "b" => array(
                "a" => "111111bb"
            )
        );
        dump($data);
        $this->assign("data2", $data);
        echo "------------------------------我是华丽丽的分割线------------------------------------<br/>";
        $data1 = array(
            "a" => array(
                "a" => $data,
                "b" => $data,
                "c" => $data
            ),
            "b" => array(
                "a" => $data,
                "b" => $data,
                "c" => $data
            ),
            "c" => array(
                "a" => $data,
                "b" => $data,
                "c" => $data
            )
        );
        dump($data1);
        $this->assign("data3", $data1);
        $this->display();
    }
}
?>